import useSWR from "swr";
import { fetcher } from "./fetcher";



const prefix = 'http://localhost:3333'

export function ListNodes() {
    return useSWR<Node[]>(`${prefix}/nodes`, fetcher);
}
export function ListConverters() {
    return useSWR<Converter[]>(`${prefix}/converters`, fetcher);
}
export function ListNotUsedConverters() {
    return useSWR<Converter[]>(`${prefix}/converters/not-used`, fetcher);
}

export function ListLocations() {
    return useSWR<Location[]>(`${prefix}/locations`, fetcher);

}

export const ListDevicesOfLocation = (id: string) => useSWR<Device[]>(`${prefix}/locations/${id}/devices`, fetcher);

export const ListConvertersOfNode = (id: string) => useSWR<Converter[]>(`${prefix}/nodes/${id}/converters`, fetcher);

export const ListSerialsOfNode = (id: string) => useSWR<[Serial]>(`${prefix}/nodes/${id}/serials`, fetcher);

export const GetState = (id: string) => useSWR(`${prefix}/state/${id}`, fetcher);

export const GetLocation = (id: string) => useSWR<Location>(`${prefix}/locations/${id}`, fetcher);

export const GetNode = (id: string) => useSWR<Node>(`${prefix}/nodes/${id}`, fetcher);

export const CreateLocation = (data: any) =>

    fetcher(`${prefix}/locations`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
    });

export const CreateSerialDevice = (data: any) =>

    fetcher(`${prefix}/devices/serial`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
    });

export const CreateConverterDevice = (data: any) =>

    fetcher(`${prefix}/devices/converter`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
    });

export const CreateWifiDevice = (data: any) =>

    fetcher(`${prefix}/devices/wifi`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
    });
export const CreateZigbeeDevice = (data: any) =>

    fetcher(`${prefix}/devices/zigbee`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
    });



export const UpdateLocation = (id: number, data: any) =>

    fetcher(`${prefix}/locations/${id}`, {
        method: "PUT",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
    });

export const UpdateDevice = (id: number, data: any) =>

    fetcher(`${prefix}/devices/${id}`, {
        method: "PUT",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
    });


export const DeleteLocation = (id: string) =>

    fetcher(`${prefix}/locations/${id}`, {
        method: "DELETE",
    });

export const DeleteDevice = (id: string) =>

    fetcher(`${prefix}/devices/${id}`, {
        method: "DELETE",
    });


export async function ValidateCredentials(data: any) {

    return await fetch(`${prefix}/login`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
    });

}

export const CreateCommand = (data: any) =>

    fetcher(`${prefix}/commands`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json"
        },
        body: JSON.stringify(data)
    });









export async function getWifiDevices() {

    return [
        { sn: "plug-1" },
        { sn: "switch-2" },


    ]

}

export async function getZigbeeDevices() {

    return [
        { sn: "12344", vendor: "tuya", type: "switch" },
        { sn: "4rfgt", vendor: "tuya", type: "panel" },


    ]

}
